<template>
  <div>
    <el-form ref="attachment" :model="baseEntity">
      <el-table
        :data="attachmentTableData"
      >
        <el-table-column
          label="附件名称"
        >
          <template slot-scope="scope">
            <i class="el-icon-document-remove"></i>
            <span style="margin-left: 10px">{{ scope.row.attachmentName }}</span>
          </template>
        </el-table-column>
        <el-table-column
          label="文件上传"
        >
          <template slot-scope="scope">
            <archive-attachment :ref="scope.row.ref" v-model="baseEntity[scope.row.ref]"
                                :business-id="adjunctId" :limit="10"
                                :uploadSizeLimit="100" :uploader-id="scope.row.uploaderId" module-name="上传"
                                @onFileRemove="onFileRemove"
                                @onFileSuccess="onFileSuccess(scope.row, $event)"
            >
            </archive-attachment>
          </template>
        </el-table-column>
      </el-table>
    </el-form>
  </div>
</template>

<script>
import { addAfterRentFile } from '@/api/hsrzzl/afterRent/archives'
import ArchiveAttachment from './archiveAttachment'

export default {
  name: 'ArchiveFormTable',
  components: {
    ArchiveAttachment
  },
  props: {
    editable: {
      type: Boolean,
      default: true
    },
    attachmentTableData: {
      required: true,
      type: Array,
      default: () => []
    },
    clientId: {
      type: Number,
      required: false,
      default: -1
    },
    projectId: {
      type: Number,
      required: false,
      default: -1
    },
    contractId: {
      type: Number,
      required: false,
      default: -1
    },
    paymentId: {
      type: Number,
      required: false,
      default: -1
    }
  },
  data() {
    return {
      baseEntity: {},
      adjunctId: -1
    }
  },
  created() {
    if (this.clientId !== -1) {
      this.adjunctId = this.clientId
    }
    if (this.projectId !== -1) {
      this.adjunctId = this.projectId
    }
    if (this.contractId !== -1) {
      this.adjunctId = this.contractId
    }
    if (this.paymentId !== -1) {
      this.adjunctId = this.paymentId
    }
  },
  watch: {
    clientId: {//深度监听，可监听到对象、数组的变化
      handler(val, oldVal) {
        if (this.clientId) {
          this.init(val)
        }
      },
      deep: true //true 深度监听
    },
    projectId: {//深度监听，可监听到对象、数组的变化
      handler(val, oldVal) {
        if (this.projectId) {
          this.init(val)
        }
      },
      deep: true //true 深度监听
    },
    contractId: {//深度监听，可监听到对象、数组的变化
      handler(val, oldVal) {
        if (this.contractId) {
          this.init(val)
        }
      },
      deep: true //true 深度监听
    },
    paymentId: {//深度监听，可监听到对象、数组的变化
      handler(val, oldVal) {
        if (this.paymentId) {
          this.init(val)
        }
      },
      deep: true //true 深度监听
    }
  },
  methods: {
    init(id) {
      const that = this
      setTimeout(() => {
        that.attachmentTableData.forEach((element, index) => {
          if (that.$refs[element.ref]) {
            that.$refs[element.ref].setBusinessId(id)
            that.$refs[element.ref].initFileList()
          }
        })
      }, 500)
    },
    onFileRemove(attachmentId) {
      if (this.baseEntity.searchValue) {
        this.baseEntity.searchValue = String(this.baseEntity.searchValue).replace(',' + attachmentId, '').replace(attachmentId, '')
      }
    },
    onFileSuccess(row, e) {
      console.log(row)
      console.log(e)
      console.log(this.baseEntity)
      let uploadAttachmentIds = e ? '' + e : ''
      this.baseEntity.searchValue = uploadAttachmentIds

      this.saveRow(row)
    },
    //保存
    saveRow(row) {
      let flag = true
      if (this.clientId !== -1) {
        this.$set(row, 'clientId', this.clientId)
        this.$set(row, 'businessId', this.clientId)
        flag = false
      }
      if (this.projectId !== -1) {
        this.$set(row, 'projectId', this.projectId)
        this.$set(row, 'businessId', this.projectId)
        flag = false
      }
      if (this.contractId !== -1) {
        this.$set(row, 'contractId', this.contractId)
        this.$set(row, 'businessId', this.contractId)
        flag = false
      }
      if (this.paymentId !== -1) {
        this.$set(row, 'paymentId', this.paymentId)
        this.$set(row, 'businessId', this.paymentId)
        flag = false
      }
      if (!flag) {
        let selectedRowData = JSON.parse(JSON.stringify(this.baseEntity))
        for (let p in selectedRowData) {
          row[p] = selectedRowData[p] //将sel里面的value赋值给这一行
        }
        console.log(row)
        addAfterRentFile(row).then((response) => {
          if (response) {
            if (response.code === 200) {
              row.status = false
              // this.reload()
            } else {
              this.msgError(response.msg)
            }
          }
        })
      }
    }
  }
}
</script>

<style scoped>

</style>
